/**
 * Copyright (c) 2009 Cerner Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cerner Corporation - initial API and implementation
 */
package com.cerner.system.rest.internal.arguments;

import java.util.List;

/**
 * Abstract base class for all artifacts that check the validity of method or
 * constructor arguments.
 * 
 * @author Alex Horn
 */
abstract class AbstractValidator implements Validator {

  /**
   * Construct an error message that can be used to construct a string for an
   * {@link IllegalArgumentException}. The argument name list must have at least
   * one item in it.
   * 
   * @param names non-empty list of names for method arguments whose values did
   *          not comply with the rules
   * @return human-readable CSV of the argument names
   */
  String concat(List<String> names) {
    /* Create string of the form: "Arguments 'foo', 'bar' and 'baz'" */
    final StringBuilder builder = new StringBuilder(8 + 16 * names.size());
    builder.append("Argument");

    /* pluralize the noun if there are multiple argument names */
    if (names.size() > 1) {
      builder.append("s");

      /* delimit all argument names by a comma except the last two ones */
      for (int i = 0; i < names.size() - 2; i++) {
        builder.append(" '");
        builder.append(names.get(i));
        builder.append("',");
      }

      /* conditional check ensures that there are at least two elements */
      builder.append(" '");
      builder.append(names.get(names.size() - 2));
      builder.append("' and");
    }

    /* append the last argument name without a delimiting comma */
    builder.append(" '");
    builder.append(names.get(names.size() - 1));
    builder.append("'");
    return builder.toString();
  }
}
